package com.webull.information.center.carwler.common.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.webull.information.center.carwler.common.util.NewsCcUtil;

/**
 * 原始资讯
 *
 * @author shimingjun
 * @since 0.1.0
 */
public class NewsInformation {

    /**
     *
     */

    private Long newsId;//
    private String storeShard;
    private String title;// ` varchar(100) NOT NULL COMMENT '新闻标题',
    private String summary;// ` varchar(100) DEFAULT NULL COMMENT '摘要',
    private Integer siteType;// '资源类型:0,普通网页,1为聚合类型';
    private String content;// ` text COMMENT '新闻正文',

    private MainPic mainPic = new MainPic();//缩略图
    private ContextPic contextPic = new ContextPic();//主图

    private Date newsTime;// ` timestamp NULL DEFAULT NULL COMMENT '新闻时间',
    private TimeZone newsTimeZone;// news 当地timeZone
    private String pushTime;
    private Integer regionId;
    private String sourceName;// ` varchar(100) DEFAULT NULL COMMENT '新闻来源',
    private String language;
    private Integer homePage;//网页功能
    private String urlMd5;
    private String url;// ` varchar(200) DEFAULT NULL COMMENT '原文链接地址',

    private String collectSource;// ` int(11) DEFAULT NULL COMMENT '采集来源',
    private ArticleClass articleClass;

	private boolean forceView = false;// 强制更新
    private List<AssociatesNews> associates = new ArrayList<>();// 关联的新闻

	private Map<String, String> notes = new HashMap<>(); // 附加信息，过程处理标记等

    public ArticleClass getArticleClass() {
        return articleClass;
    }

    public void setArticleClass(ArticleClass articleClass) {
        this.articleClass = articleClass;
    }

    public TimeZone getNewsTimeZone() {
        return newsTimeZone;
    }

    public void setNewsTimeZone(TimeZone newsTimeZone) {
        this.newsTimeZone = newsTimeZone;
    }

    public String getSumMd5() {
		return NewsCcUtil.genTitleMd5(StringUtils.removePattern(getTitle() + "", "\\pP"));
    }

    public String getUrlMd5() {
        return urlMd5;
    }

    public void setUrlMd5(String urlMd5) {
        this.urlMd5 = urlMd5;
        associates.forEach(v -> bindPrimary(v));
    }

    public Integer getRegionId() {
        return regionId;
    }

    public void setRegionId(Integer regionId) {
        this.regionId = regionId;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public Long getNewsId() {
        return newsId;
    }

    public void setNewsId(Long newsId) {
        this.newsId = newsId;
        associates.forEach(v -> bindPrimary(v));
    }

    public String getTitle() {
		return StringUtils.removeStart(title, "[$$]");
    }


    public void setTitle(String title) {
        this.title = title;
    }

    public String getStoreShard() {
        return storeShard;
    }

    public void setStoreShard(String storeShard) {
        this.storeShard = storeShard;
    }

    public String getSummary() {
        return StringUtils.substring(summary, 0, 260);
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


    public int addMainPic(String mainPic) {
        if (StringUtils.isNotBlank(mainPic)) {
            this.mainPic.getMainPic().add(mainPic);
            return 1;
        }
        return 0;
    }

    public void setMainProxy(boolean mainProxy) {
        this.mainPic.setMainProxy(mainProxy);
    }

    public MainPic getMainPic() {
        return mainPic;
    }

    void setMainPic(MainPic mainPic) {
        this.mainPic = mainPic;
    }

    public Date getNewsTime() {
        return newsTime;
    }

    public void setNewsTime(Date newsTime) {
        this.newsTime = newsTime;
    }

    public String getPushTime() {
        return pushTime;
    }

    public void setPushTime(String pushTime) {
        this.pushTime = pushTime;
    }

    public String getSourceName() {
        return sourceName;
    }

    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getCollectSource() {
        return collectSource;
    }

    public void setCollectSource(String collectSource) {
        this.collectSource = collectSource;
    }

    public Integer getSiteType() {
        return siteType;
    }

    public void setSiteType(Integer siteType) {
        this.siteType = siteType;
    }

    public ContextPic getContextPic() {
        return contextPic;
    }

    public Integer getHomePage() {
        return homePage;
    }

    public void setHomePage(Integer homePage) {
        this.homePage = homePage;
    }

    public List<AssociatesNews> getAssociates() {
        return associates;
    }


	public boolean isForceReview() {
		return forceView;
	}

	public void setForceReview(boolean forceUpdate) {
		this.forceView = forceUpdate;
	}

	public String getNote(String key) {
		return notes.get(key);
	}

	public void setNote(String key, String notes) {
		this.notes.put(key, notes);
	}



	protected void setNotes(Map<String, String> notes) {
		this.notes = notes;
	}

	/**
	 * news.html文章样式选项
	 *
	 * @author shimingjun
	 * @version 1.0
	 * @date 2017年2月10日 下午4:06:45
	 * @since JDK 1.8
	 */
    public static enum ArticleClass {
        article("article"), articleEn("article en");
        private String value;

        private ArticleClass(String value) {
            this.value = value;
        }

        public String getVal() {
            return this.value;
        }
    }

    /**
     * 新闻缩略图
     */
    public static class MainPic {
        private List<String> mainPic = new ArrayList<>();// 待可选的主图列表,根据规则选取合适的。
        private boolean mainProxy = true;// 图片下载需要代理
        private Integer thumSize;

        public List<String> getMainPic() {
            return mainPic;
        }

        public void setMainPic(List<String> mainPic) {
            this.mainPic = mainPic;
        }

        public boolean isMainProxy() {
            return mainProxy;
        }

        public void setMainProxy(boolean mainProxy) {
            this.mainProxy = mainProxy;
        }

        public Integer getThumSize() {
            return thumSize;
        }

        public void setThumSize(Integer thumSize) {
            this.thumSize = thumSize;
        }
    }

    /**
     * 新闻主图
     */
    public static class ContextPic {
        private String contextPic = null;// 待可选的内容图片,根据规则选取合适的。
        private boolean contextProxy = true;// 待可选的内容图片,根据规则选取合适的。

        public String getContextPic() {
            return contextPic;
        }

        public void setContextPic(String contextPic) {
            this.contextPic = contextPic;
        }

        public boolean isContextProxy() {
            return contextProxy;
        }

        public void setContextProxy(boolean contextProxy) {
            this.contextProxy = contextProxy;
        }
    }


    public void addAssociatesNews(AssociatesNews assoc) {
        associates.add(assoc);
    }

    public AssociatesNews derivedAssociate() {
        AssociatesNews assoc = new AssociatesNews();
        BeanUtils.copyProperties(this, assoc);
        bindPrimary(assoc);
        clearPrivate(assoc);
        return assoc;
    }

    /**
     * 绑定所属主键
     *
     * @param assoc
     */
	private void bindPrimary(AssociatesNews assoc) {
        assoc.setAssNewsId(this.newsId);
        assoc.setAsslMd5(this.urlMd5);
    }

    /**
     * 清空私有属性
     *
     * @param assoc
     */
    private void clearPrivate(AssociatesNews assoc) {
        assoc.setContent(null);
        assoc.setNewsTime(null);
        assoc.setTitle(null);
        assoc.setSummary(null);
        assoc.setUrl(null);
        assoc.setUrlMd5(null);
        assoc.setStoreShard(null);
        assoc.setMainPic(new MainPic());
		assoc.setForceReview(false);
		assoc.setNotes(new HashMap<>());
    }

    /**
     * 关联新闻功能
     */
    public static class AssociatesNews extends NewsInformation {

        private String asslMd5;// 关联所属新闻的md5
        private Long assNewsId;//关联所属新闻的newsId

        public String getAsslMd5() {
            return asslMd5;
        }

        public void setAsslMd5(String asslMd5) {
            this.asslMd5 = asslMd5;
        }

        public Long getAssNewsId() {
            return assNewsId;
        }

        public void setAssNewsId(Long assNewsId) {
            this.assNewsId = assNewsId;
        }
    }
}
